local Widget          = require "widgets/widget"
local Text            = require "widgets/text"
local UIAnim          = require "widgets/uianim"

local TINT            = { 1, 1, 1, 1 }
local FONT            = TALKINGFONT or HEADERFONT

-- 刺客567 等级 / 经验 HUD 小组件
local AssnLevelWidget = Class(Widget, function(self, owner)
    self.owner = owner
    Widget._ctor(self, "AssnLevelWidget")

    -- 等级数字
    self.level_text = self:AddChild(Text(FONT, 30, "Lv 0", TINT))
    self.level_text:SetPosition(-40, 32)

    -- 经验条
    self.bar_root = self:AddChild(Widget("BarRoot"))
    self.bar_root:SetPosition(0, 10)

    self.bar_bg = self.bar_root:AddChild(Image("images/global.xml", "square.tex"))
    self.bar_bg:SetTint(0, 0, 0, .4)
    self.bar_bg:SetSize(120, 14)

    self.bar_fg = self.bar_root:AddChild(Image("images/global.xml", "square.tex"))
    self.bar_fg:SetTint(0.9, 0.8, 0.2, 1)
    self.bar_fg:SetSize(0, 14)
    self.bar_fg:SetPosition(-60, 0) -- 左对齐裁切

    self.bar_fg:SetVRegPoint(ANCHOR_MIDDLE)
    self.bar_fg:SetHRegPoint(ANCHOR_LEFT)

    -- 经验数字
    self.exp_text = self:AddChild(Text(FONT, 20, "exp 0/100", TINT))
    self.exp_text:SetPosition(20, 30)
end)

-- 刷新函数
function AssnLevelWidget:OnUpdate(level, cur_exp, max_lv)
    -- 等级
    self.level_text:SetString(string.format("Lv %d", level))
    -- 经验
    self.exp_text:SetString(string.format("exp %d/%d", cur_exp, max_lv))
    -- 经验条
    local pct = 0
    if level < max_lv then
        pct = (cur_exp % 100) / 100 -- 0~1
    end
    self.bar_fg:SetSize(120 * pct, 14)
end

return AssnLevelWidget
